﻿using System;
using System.Collections.Generic;
using System.DirectoryServices;
using System.Web.UI;
using shfa.snomtastic.admin.components;
using shfa.snomtastic.data.business;
using shfa.snomtastic.data.security;
using Telerik.Web.UI;

namespace shfa.snomtastic.admin.editcontrols
{
    public partial class eShowConfig : EditBase
    {
        #region Public Members

        public AppConfig ac = new AppConfig();

        public Guid SelectedItemId
        {
            get { return (hfSelectedItemId.Value != string.Empty) ? new Guid(hfSelectedItemId.Value) : Guid.Empty; }
            set { hfSelectedItemId.Value = value.ToString(); }
        }

        #endregion

        #region Page

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Page_Init(object sender, EventArgs e)
        {
            if (ItemId != Guid.Empty)
            {
                SelectedItemId = ItemId;
                BindGridItems();
                BuildScript();
            }

           
            



            rgListItems.ItemCommand += new GridCommandEventHandler(rgListItems_ItemCommand);

        }



        #endregion

        #region Event Handlers

        protected void rgListItems_ItemCommand(object source, GridCommandEventArgs e)
        {
            if (e.CommandName == "Sort" | e.CommandName == "Filter")
            {
                BindGridItems();
            
            }

            if (e.CommandName == "Edit")
            {
                RaiseNavEvent(this, new NavEventArgs("ePhone", new Guid()));
            }
        }

        #endregion


        private void BuildScript()
        {

            string script = "";

            script += "function OnRowDblClick(sender, eventArgs)   ";
            script += "{";
            script += "sender.get_masterTableView().editItem(eventArgs.get_itemIndexHierarchical());";
            script += "}     ";


            ScriptManager.RegisterClientScriptBlock(this, typeof(EditBase), "test", script, true);
        }

        public void BindGridItems()
        {
            BindGridItems("");
        }
        public void BindGridItems(string sort)
        {
            PhoneController pc = new PhoneController();
            PhoneInfo pi = pc.SelectPhone(SelectedItemId);


            List<ConfigurationSettingInfo> csiBind = new List<ConfigurationSettingInfo>();


            if (pi != null)
            {
                lblHeaderLeft.Text = "Phone - " + pi.MacAddress;
                lblHeaderRight.Text = "[No User]";

                UserController uc = new UserController();
                UserInfo ui = uc.SelectUser(pi.UserId);

                if (ui != null)
                {

                    lblHeaderRight.Text = ui.UserName;

                    DirectoryEntry adUser = GetADUser(ui.UserName);

                    ConfigurationSettingController csc = new ConfigurationSettingController();
                    List<ConfigurationSettingInfo> csiList = csc.GetConfigurationSettingsForPhone(pi.MacAddress);
                    SettingController sc = new SettingController();

                    if (csiList != null)
                    {

                        foreach (ConfigurationSettingInfo csi in csiList)
                        {
                            if (csi.Active)
                            {
                                SettingInfo si = sc.SelectSetting(csi.SettingId);
                                if (si != null)
                                {
                                    csi.FillDetails();
                                    csi.Value = shfa.snomtastic.data.common.GetFormattedValue(csi, ui, adUser, pi.MacAddress, pi.IpAddress, si.Encrypted, false);
                                    csiBind.Add(csi);
                                }
                            }
                        }
                    }
                }
            }

            if (sort!=string.Empty)
            {
                csiBind.Sort(new ConfigurationSettingComparerBI(sort));
            }
            


            rgListItems.DataSource = csiBind;
            rgListItems.DataBind();


        }


        #region Action Events


        #endregion

        #region Helpers



        private DirectoryEntry GetADUser(string upn)
        {
            adSecurity ad = new adSecurity();
            if (upn != string.Empty)
            {
                DirectoryEntry de = ad.GetUserByUPN(upn);
                if (de != null)
                    return de;
            }

            return null;
        }


        #endregion



        #region EditBase

        public override void EditBase_ActionEvent(object sender, ActionClickArgs e)
        {
            switch (e.ActionName)
            {
                case "Refresh":
                    RaiseNavEvent(this, new NavEventArgs("eShowConfig", SelectedItemId));
                    break;
                case "Return":
                    RaiseNavEvent(this, new NavEventArgs("ePhone", SelectedItemId));
                    break;
                default:
                    break;
            }
        }

        public override List<ActionInfo> ActionList
        {
            get
            {
                List<ActionInfo> al = new List<ActionInfo>();
                al.Add(new ActionInfo("Refresh", "Refresh Settings", "~/images/icons/refresh.png"));
                al.Add(new ActionInfo("Return", "Return", "~/images/icons/undo.png"));

                return al;
            }
        }

        #endregion

    }
}